/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2019年1月7日
 */

package com.massyframework.beanskin.spring.beancontext;


import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.DefaultResourceLoader;

import com.massyframework.beanskin.assembly.Assembly;
import com.massyframework.beanskin.spring.beancontext.lazyinit.LazyInitConfigurerXmlApplicationContext;



/**
 * {@link StandloneApplicationContextFactory} 提供非web运行场景下的ApplicationContext
 */
public final class StandloneApplicationContextFactory
        extends AbstractApplicationContextFactory<ConfigurableApplicationContext> {

    public StandloneApplicationContextFactory() {
    }

    /**
     * 根据<code>assembly</code>创建Spring 应用程序上下文
     *
     * @param assembly    {@link Assembly}, 装配件
     * @param initializer {@link ApplicationContextInitializer}, 应用程序上下文初始化器
     * @return {@link ConfigurableApplicationContext}
     * @throws ClassNotFoundException 
     */
    @Override
    public ConfigurableApplicationContext createApplicationContext(Assembly assembly,
                  ApplicationContextInitializer<ConfigurableApplicationContext> initializer){

        Class<?>[] annoTypes = this.getAnnotationTypes(assembly);

        String text = assembly.getConfig().getParameter("lazy.init", "true");
        Boolean defaultLazyInit = text == null ? null : Boolean.parseBoolean(text);
       

        ConfigurableApplicationContext result =
                annoTypes.length != 0 ?
                    new AnnotationConfigApplicationContext() :
                        new LazyInitConfigurerXmlApplicationContext(defaultLazyInit);

        if (initializer != null){
            initializer.initialize(result);
        }
        
        if (result instanceof DefaultResourceLoader) {
        	((DefaultResourceLoader)result).setClassLoader(assembly.getModuleClassLoader());
        }

        if (result instanceof AnnotationConfigApplicationContext){
            ((AnnotationConfigApplicationContext)result).register(annoTypes);
        }else{
            ((ClassPathXmlApplicationContext)result).setConfigLocations(this.getConfigLocations(assembly));
        }

        return result;
    }
}
